package com.olympia.myapp.xml;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.olympia.myapp.entity.Member;
import com.olympia.myapp.entity.QuestionKD;
import com.olympia.myapp.entity.QuestionTT;
import com.olympia.myapp.entity.QuestionVCNV;
import com.olympia.myapp.entity.QuestionVD;

public class QuestionXMLHandler extends DefaultHandler {

	Boolean currentElement = false;
	String currentValue = null;
	public static List<QuestionKD> qsKD = null;
	QuestionKD kdQS = null;
	public static List<QuestionVCNV> qsVCNV = null;
	QuestionVCNV vcnvQS = null;
	QuestionVCNV vcnvQSFull = null;
	public static List<QuestionTT> qsTT = null;
	List<Member> members = null;
	Member member = null;
	QuestionTT ttQS = null;
	public static List<QuestionVD> qsVD = null;
	QuestionVD vdQS = null;
	int typeQS = 0;// 0 KD, 1 VCNV, 2 TT, 3 VD
	String idQS = "";
	String content = "";
	String answerContent = "";
	String answer = "";
	String keyIndex = "";

	/**
	 * http://www.azsoft.com.vn/myapp/nauan/server.ashx?action=OLPAQUEST&version
	 * =FULL&email=tatuanvn@gmail.com&pass=123456
	 */
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		currentElement = true;

		if (localName.equals("KHOIDONG")) {
			typeQS = 0;
			qsKD = new ArrayList<QuestionKD>();
		} else if (localName.equals("VCNV")) {
			typeQS = 1;
			qsVCNV = new ArrayList<QuestionVCNV>();
		} else if (localName.equals("itemwords")) {
			typeQS = 1;
			vcnvQSFull = new QuestionVCNV();
		} else if (localName.equals("TANGTOC")) {
			typeQS = 2;
			qsTT = new ArrayList<QuestionTT>();
		} else if (localName.equals("members" + idQS)) {
			typeQS = 2;
			members = new ArrayList<Member>();
		} else if (localName.equals("member" + idQS)) {
			typeQS = 2;
			member = new Member();
		} else if (localName.equals("VEDICH")) {
			typeQS = 3;
			qsVD = new ArrayList<QuestionVD>();
			vdQS = new QuestionVD();
		}
	}

	/*
	 * 
	 */
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {

		currentElement = false;

		/** set value */
		switch (typeQS) {
		case 0: {
			if (localName.equalsIgnoreCase("questionsId"))
				idQS = currentValue;
			else if (localName.equalsIgnoreCase("content"))
				content = currentValue;
			else if (localName.equalsIgnoreCase("answers"))
				answerContent = currentValue;
			else if (localName.equalsIgnoreCase("answerAB"))
				answer = currentValue;
			else if (localName.equalsIgnoreCase("item")) {
				kdQS = new QuestionKD(idQS, content, answerContent, answer);
				qsKD.add(kdQS);
			}
			break;
		}
		case 1:
			if (localName.equalsIgnoreCase("questionsId"))
				idQS = currentValue;
			else if (localName.equalsIgnoreCase("content"))
				content = currentValue;
			else if (localName.equalsIgnoreCase("words"))
				answer = currentValue.trim().replace(" ", "");
			else if (localName.equalsIgnoreCase("keyIndex"))
				keyIndex = currentValue;
			else if (localName.equalsIgnoreCase("item")) {
				vcnvQS = new QuestionVCNV(idQS, content, answer, keyIndex);
				qsVCNV.add(vcnvQS);
			}
			// tu hang doc
			else if (localName.equalsIgnoreCase("wordsfull"))
				vcnvQSFull.words = currentValue;
			else if (localName.equalsIgnoreCase("contentWordVertical"))
				vcnvQSFull.content = currentValue;
			else if (localName.equalsIgnoreCase("itemwords"))
				qsVCNV.add(vcnvQSFull);

			break;
		case 2:
			if (localName.equalsIgnoreCase("questionsId"))
				idQS = currentValue;
			else if (localName.equalsIgnoreCase("content"))
				content = currentValue;
			else if (localName.equalsIgnoreCase("answers"))
				answerContent = currentValue.replace("@#@", "\n");
			else if (localName.equalsIgnoreCase("answerABCD"))
				answer = currentValue;
			else if (localName.equalsIgnoreCase("fullname"))
				member.name = currentValue;
			else if (localName.equalsIgnoreCase("answerABCD" + idQS))
				member.ansContet = currentValue;
			else if (localName.equalsIgnoreCase("answersecond"))
				member.ansTime = currentValue;
			else if (localName.equalsIgnoreCase("member" + idQS))
				members.add(member);
			else if (localName.equalsIgnoreCase("item")) {
				ttQS = new QuestionTT(idQS, content, answerContent, answer,
						members);
				qsTT.add(ttQS);
			}
			break;
		case 3:
			if (localName.equalsIgnoreCase("questionsId"))
				vdQS.id = currentValue;
			else if (localName.equalsIgnoreCase("content"))
				vdQS.content = currentValue;
			else if (localName.equalsIgnoreCase("answers"))
				vdQS.answerContent = currentValue.replace("@#@", "\n");
			else if (localName.equalsIgnoreCase("answerABCD"))
				vdQS.answer = currentValue;
			else if (localName.equalsIgnoreCase("score"))
				vdQS.score = currentValue;
			else if (localName.equalsIgnoreCase("item")) {
				qsVD.add(vdQS);
				vdQS = new QuestionVD();
			}
			break;
		default:
			break;
		}
	}

	/**
	 * Called to get tag characters ( ex:- <name>AndroidPeople</name> -- to get
	 * AndroidPeople Character )
	 */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {

		if (currentElement) {
			currentValue = new String(ch, start, length);
			currentElement = false;
		}

	}

}
